local rule = fk.CreateSkill {
  name = "#hx__role_rule&",
}

Fk:loadTranslationTable{
  ["#role_rule"] = "你可以将体力回复至1",
  ["@!!azure_dragon"] = "青龙",
  ["@!!white_tiger"] = "白虎",
  ["@!!vermilion_bird"] = "朱雀",
  ["@!!black_tortoise"] = "玄武",
  [":@!!azure_dragon"] = "回合开始时，你可以弃置两张牌，弃置你判定区里的一张【乐不思蜀】或【兵粮寸断】。",
  [":@!!white_tiger"] = "你可以将一张牌当【杀】或【闪】使用或打出。",
  [":@!!vermilion_bird"] = "出牌阶段，你可以弃置一张非基本牌，对一名角色造成1点伤害。以此法杀死反贼不执行奖惩。",
  [":@!!black_tortoise"] = "你可以将一张牌当【桃】使用。",

  ["#lose_four_symbol_mark"] = "所有反贼死亡，主公失去 %arg 标记"
}

rule:addEffect("maxcards", {
  correct_func = function (self, player)
    if player.role == "lord" then
      return Fk:currentRoom():getSettings("lord_extra_maxHancard")
    end
  end
})

--后置位初始手牌+1，10号位+2
rule:addEffect(fk.DrawInitialCards, {
  priority = 0.001,
  can_refresh = function(self, event, target, player, data)
    return target == player and player.seat >= 8
  end,
  on_refresh = function(self, event, target, player, data)
    data.num = data.num + (Fk:currentRoom():getSettings("late_position_initialHandcard") or 0)
  end,
})

rule:addEffect(fk.AskForPeachesDone, {
  mute = true,
  can_trigger = function(self, event, target, player, data)
    return target == player and player.room:getSettings("renegadeComeBackToLife") and 
      player.hp <= 0 and player:getMark("huixiang__come_back_to_life") > 0 and player.role == "renegade"
  end,
  on_cost  = function (self, event, target, player, data)
    return player.room:askToSkillInvoke(player, {skill_name = "game_rule", prompt = "#role_rule"})
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    room:recover{
      who = player,
      num = 1 - player.hp,
      recoverBy = player,
      skillName = "game_rule",
    }
    room:setPlayerMark(player, "huixiang__come_back_to_life", 1)
    room:setPlayerProperty(player, "role_shown", true)
  end,
})

rule:addEffect(fk.GameStart, {
  mute = true,
  can_trigger = function (self, event, target, player, data)
    local room = player.room
    return 
  end,
  on_cost = Util.TrueFunc,
  on_use = function (self, event, target, player, data)
    local room = player.room
    local mark_type = math.random(4)
    local mark_name_table = {
      "azure_dragon",
      "white_tiger",
      "vermilion_bird",
      "black_tortoise"
    }
    room:doSuperLightBox("packages/hx_gamemode/qml/DivineBeastFly.qml", {playerId = player.id, beastType = mark_type - 1})
    room:delay(1700)
    room:setPlayerMark(player, "@!!"..mark_name_table[mark_type], 1)
    player:addFakeSkill("hx__"..mark_name_table[mark_type].. "&")
    player:prelightSkill("hx__"..mark_name_table[mark_type].. "&", true)
  end
})

rule:addEffect(fk.TurnEnd, {
  mute = true,
  can_refresh = function (self, event, target, player, data)
    local room = player.room
    return not table.find(room:getAlivePlayers(false), function (p)
      return p.role == "rebel"
    end) and player:getMark("@!!black_tortoise") > 0
  end,
  on_refresh = function (self, event, target, player, data)
    local room = player.room
    room:setPlayerMark(player, "@!!black_tortoise", 0)
    if player:isFakeSkill("hx__black_tortoise&") then
      player:loseFakeSkill("hx__black_tortoise&")
    end
    room:sendLog{
      type = "#lose_four_symbol_mark",
      arg = "@!!black_tortoise",
      toast = true
    }
  end
})

rule:addEffect(fk.TurnEnd, {
  mute = true,
  can_refresh = function (self, event, target, player, data)
    local room = player.room
    return not table.find(room:getAlivePlayers(false), function (p)
      return p.role == "rebel"
    end) and player:getMark("@!!azure_dragon") > 0
  end,
  on_refresh = function (self, event, target, player, data)
    local room = player.room
    room:setPlayerMark(player, "@!!azure_dragon", 0)
    if player:isFakeSkill("hx__azure_dragon&") then
      player:loseFakeSkill("hx__azure_dragon&")
    end
    room:sendLog{
      type = "#lose_four_symbol_mark",
      arg = "@!!azure_dragon",
      toast = true
    }
  end
})

rule:addEffect(fk.TurnEnd, {
  mute = true,
  can_refresh = function (self, event, target, player, data)
    local room = player.room
    return not table.find(room:getAlivePlayers(false), function (p)
      return p.role == "rebel"
    end) and player:getMark("@!!white_tiger") > 0
  end,
  on_refresh = function (self, event, target, player, data)
    local room = player.room
    room:setPlayerMark(player, "@!!white_tiger", 0)
    if player:isFakeSkill("hx__white_tiger&") then
      player:loseFakeSkill("hx__white_tiger&")
    end
    room:sendLog{
      type = "#lose_four_symbol_mark",
      arg = "@!!white_tiger",
      toast = true
    }
  end
})

rule:addEffect(fk.TurnEnd, {
  mute = true,
  can_refresh = function (self, event, target, player, data)
    local room = player.room
    return not table.find(room:getAlivePlayers(false), function (p)
      return p.role == "rebel"
    end) and player:getMark("@!!vermilion_bird") > 0
  end,
  on_refresh = function (self, event, target, player, data)
    local room = player.room
    room:setPlayerMark(player, "@!!vermilion_bird", 0)
    if player:isFakeSkill("hx__vermilion_bird&") then
      player:loseFakeSkill("hx__vermilion_bird&")
    end
    room:sendLog{
      type = "#lose_four_symbol_mark",
      arg = "@!!vermilion_bird",
      toast = true
    }
  end
})

return rule
